﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using TIMSComponents.Choisers;
namespace TIMSComponents
{
    public class DataAccessLayer
    {     
        public DataTable GetDataFromSQL( DataTable  DataTableStructure, string SqlSelectCommand)
        {
            if (SqlSelectCommand != null)
            {
                DataTable Dt = DataTableStructure;
                SqlConnection SqlConn1 = new SqlConnection(globalC.ConnectionString);
                SqlCommand SqlCmd1 = new SqlCommand(SqlSelectCommand, SqlConn1);
                SqlCmd1.CommandType = CommandType.Text;
                SqlDataAdapter SqlDA1 = new SqlDataAdapter(SqlCmd1);
                //Fill data table from MSsql
                try
                {                    
                    Dt.Clear();
                    SqlDA1.Fill(Dt);
                    return Dt;
                }
                catch (SqlException Exc)
                {
                    MessageBox.Show(Exc.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
            }
            else
            {
                return null;
            }
            
        }
        //Overload procedure with  parameter, wherecondition 
        public DataTable GetDataFromSQL(DataTable DataTableStructure, string SqlSelectCommand, string WhereCondition)
        {
            if (SqlSelectCommand != null)
            {
                DataTable Dt = DataTableStructure;
                SqlConnection SqlConn1 = new SqlConnection(globalC.ConnectionString);
                SqlCommand SqlCmd1 = new SqlCommand(SqlSelectCommand + " where " + WhereCondition, SqlConn1);
                SqlCmd1.CommandType = CommandType.Text;
                SqlDataAdapter SqlDA1 = new SqlDataAdapter(SqlCmd1);
                //Fill data table from MSsql
                try
                {
                    Dt.Clear();
                    SqlDA1.Fill(Dt);
                    return Dt;
                }
                catch (SqlException Exc)
                {
                    MessageBox.Show(Exc.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public DataView GetFilteredData(DataTable DataSource, string ExprString)
        {
            try
            {
                DataView dw = new DataView(DataSource);
                dw.RowFilter = ExprString; //ExprString.Replace("'".ToCharArray(),ch);
                return dw;
            }
            catch(EvaluateException e1)
            {
                MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
    }
}
